import {
  reqAddOrUpdateShopCart,
  reqGoodInfo
} from '../../api'
import {
  getUUID
} from '../../utils/uuid_token'
const state = {
  goodInfo: {},
  uuid_token: getUUID()
}
const mutations = {
  GOODINFO(state, goodInfo) {
    state.goodInfo = goodInfo
  }
}
const actions = {
  async getGoodInfo({
    commit
  }, skuId) {
    let result = await reqGoodInfo(skuId)
    if (result.code == 200) {
      commit('GOODINFO', result.data)
    }
  },
  async addOrUpdateShopCart({
    commit
  }, {
    skuId,
    skuNum
  }) {
    let result = await reqAddOrUpdateShopCart(skuId, skuNum)
    if (result.code == 200) {
      return "ok";
    } else {
      return Promise.reject(new Error("faile"));
    }
  }
}
//相当于计算属性，简化数据
const getters = {
  categoryView(state) {
    return state.goodInfo.categoryView || {}
  },
  skuInfo(state) {
    return state.goodInfo.skuInfo || {}
  },
  spuSaleAttrList(state) {
    return state.goodInfo.spuSaleAttrList || []
  }
}
export default ({
  state,
  mutations,
  actions,
  getters
})